package com.zhang.service;

import com.zhang.entity.vo.*;
import com.zhang.util.DeviceOperateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * User: 张
 * Date: 2024-11-14
 * Time: 18:55
 * Description:
 */
@Service
public class MachineService {
    @Autowired
    private DeviceOperateUtil deviceOperateUtil;
    HashMap<String, String> map = null;
    /**
     * 3.1 初始化机器
     * @return
     */
    public String Init() throws Exception {
        return deviceOperateUtil.DeviceOperateMachine("init",new HashMap<>());
    }

    /**
     * 3.2 检查物料
     * @return
     */
    public String CheckMaterial() throws Exception {
        return deviceOperateUtil.DeviceOperateMachine("check_material",new HashMap<>());
    }

    /**
     * 3.3 控制传送带
     * @return
     */
    public String ControlConveyorBelt(ControlConveyorBeltBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--direction",parameter.getDirection());
        map.put("--start",parameter.getStart());
        return deviceOperateUtil.DeviceOperateMachine("control_conveyor_belt",map);
    }

    /**
     * 3.4 控制灯光
     * @return
     */
    public String ControlLight(ControlLightBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--red",parameter.getRed().toString());
        map.put("--yellow",parameter.getYellow().toString());
        map.put("--green",parameter.getGreen().toString());
        map.put("--brightness",parameter.getBrightness().toString());
        return deviceOperateUtil.DeviceOperateMachine("control_light",map);
    }
    /**
     * 3.5 控制蜂鸣器
     * @return
     */
    public String ControlBuzzer(ControlBuzzerBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--buzzer_on",parameter.getBuzzerOn().toString());
        return deviceOperateUtil.DeviceOperateMachine("control_buzzer",map);
    }

    /**
     * 3.6 推出物料
     * @return
     */
    public String PushMaterial() throws Exception {
        return deviceOperateUtil.DeviceOperateMachine("push_material",new HashMap<>());
    }
    /**
     * 3.7 运行传送带
     * @return
     */
    public String RunConveyorBelt() throws Exception {
        return deviceOperateUtil.DeviceOperateMachine("run_conveyor_belt",new HashMap<>());
    }
    /**
     * 3.8 设置轴速度
     * @return
     */
    public String SetAxisSpeed(SetAxisSpeedBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--axis",parameter.getAxis());
        map.put("--speed",parameter.getSpeed().toString());
        return deviceOperateUtil.DeviceOperateMachine("set_axis_speed",map);
    }
    /**
     * 3.9 轴回原点
     * @return
     */
    public String MoveAxisToOrigin(MoveAxisToOriginBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--axis",parameter.getAxis());
        return deviceOperateUtil.DeviceOperateMachine("move_axis_to_origin",map);
    }

    /**
     * 3.10 移动轴
     * @return
     */
    public String MoveAxis(MoveAxisBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--axis",parameter.getAxis());
        map.put("--direction",parameter.getDirection());
        map.put("--material_action",parameter.getMaterial_action());
        return deviceOperateUtil.DeviceOperateMachine("move_axis",map);
    }

    /**
     * 3.11 停止轴
     * @return
     */
    public String StopAxis(StopAxisBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--axis",parameter.getAxis());
        return deviceOperateUtil.DeviceOperateMachine("stop_axis",map);
    }

    /**
     *3.12 转换开关
     * @return
     */
    public String ToggleSwitches() throws Exception {
        return deviceOperateUtil.DeviceOperateMachine("toggle_switches",new HashMap<>());
    }
    /**
     *3.13 控制轴位置
     * @return
     */
    public String ControlAxisPosition(ControlAxisPositionBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--x_pulse",parameter.getX_pulse().toString());
        map.put("--y_pulse",parameter.getY_pulse().toString());
        return deviceOperateUtil.DeviceOperateMachine("control_axis_position",map);
    }
    /**
     * 3.14 读取轴位置
     * @return
     */
    public String ReadAxisPosition(ReadAxisPositionBody parameter) throws Exception {
        map = new HashMap<>();
        map.put("--axis",parameter.getAxis());
        return deviceOperateUtil.DeviceOperateMachine("read_axis_position",map);
    }
    /**
     * 3.15 获取传送带状态
     * @return
     */
    public String GetConveyorStatus() throws Exception {
        return deviceOperateUtil.DeviceOperateMachine("get_conveyor_status",new HashMap<>());
    }
}
